<template>
  <div>

    <el-dialog v-model="formDelete.dialog" title="确认" width="30%" center>
      <template #default>您将删除条目:{{ formDelete.name }}</template>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="formDelete.dialog = false">取消</el-button>
        <el-button type="danger" @click="Confirmdelete">确定</el-button>
      </span>
      </template>
    </el-dialog>

    <el-dialog v-model="formDetail.dialog" title="Shipping address" width="800">
      <el-table :data="formDetail.value">
        <el-table-column property="name" label="名称" width="150"/>
        <el-table-column property="price" label="价格" width="200"/>
        <el-table-column property="number" label="数量"/>
      </el-table>
    </el-dialog>

    <!--修改弹窗-->
    <el-dialog v-model="formFinish.dialog" :title="formFinish.PopupType" width="40%" center>

      <template #footer>
      <span class="dialog-footer">
        <el-button @click="formFinish.dialog=false">取消</el-button>
        <el-button type="primary" @click="ConfirmFinish">确认</el-button>
      </span>
      </template>
    </el-dialog>
    <!--table显示-->

    <el-scrollbar>
      <el-table v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}" border
                :row-style="{height: '54.7px'}"
                style="font-size: 15px" stripe :data="tableData">
        <!--        <el-table-column prop="tid" label="图书类型编号" width="100"/>-->
        <el-table-column label="状态" width="status">
          <!--订单状态 1已付款，2已出餐待派送，3已接单，4已派送，5已完成，6已取消-->
          <template #default="scopeEdit">
            <label>{{ getStatusText(scopeEdit.row.status) }}</label>
          </template>
        </el-table-column>

        <el-table-column label="收货地" prop="addressBookName" width="auto"/>
        <el-table-column label="外卖员" prop="employeeName" width="auto"/>
        <el-table-column label="时间" prop="checkoutTime" width="auto"/>
        <el-table-column label="备注" prop="remark" width="auto"/>

        <el-table-column fixed="right" label="操作">
          <template #default="scopeEdit"  > <!--   -->
            <el-button
                v-if="scopeEdit.row.status===1"
                link
                type="success"
                size="small"
                @click.prevent="finishInitialize(scopeEdit.row)">
              <el-icon>
                <Edit/>
              </el-icon>
              <span>出餐</span>
            </el-button>
            <el-button
                link
                type="primary"
                size="small"
                @click.prevent="showDetail(scopeEdit.row)">
              <el-icon>
                <Search/>
              </el-icon>
              <span>查看</span>
            </el-button>
<!--            <el-button-->
<!--                link-->
<!--                type="danger"-->
<!--                size="small"-->
<!--                @click.prevent="deleteInitialize(scopeEdit.row)">-->
<!--              <el-icon>-->
<!--                <Delete/>-->
<!--              </el-icon>-->
<!--              <span>删除</span>-->
<!--            </el-button>-->
          </template>
        </el-table-column>

      </el-table>
    </el-scrollbar>

  </div>
</template>

<script lang="ts" setup>

import {onMounted, reactive, ref, watch} from "vue";
import {Delete, Edit, Operation, Plus, Search} from '@element-plus/icons-vue'


import {ElMessage, UploadProps} from "element-plus";
import {ElLoading} from "element-plus/es";
import {addMenuApi, deleteMenuApi, getDishApi, updateApi} from "@/api/Menu";
import {getStatusText, getToken} from "@/utils/auth";
import {editOrderApi, getOrderByBusinessIdApi} from "@/api/Order";

const PopupType = ref(['添加', '出餐'])

//数据初始化
onMounted(() => {
  getTableData()
})

const rolesList = ref<any[]>([])
//分页
const page = reactive({
  current: 1,
  limit: 10,
  total: 0,
})


const tableData = ref([])
const loading = ref(true)
const search = reactive({
  author: "",
  bookName: "",
  tid: ""
})

function getTableData() {
  getOrderByBusinessIdApi().then((response: any) => {
    if (response.code === 200) {
      tableData.value = response.data

    } else {
      ElMessage.warning(response.message)
    }
    loading.value = false
  }).catch(function (res) {
    loading.value = false
    ElMessage.error("页面加载失败")
  })
}


//添加修改数据初始化
const BtnConfirmAble = ref(true)
const formFinish = reactive({
  addressBookName: '',
  amount: '',
  businessName: "",
  cabinetAddress: "",
  checkoutTime: "",
  employeeName: "",
  id: 1,
  number: null,
  orderTime: "",
  remark: "",
  status: 2,
  userId: 5,
  PopupType: "",
  dialog: false
})


const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('图片必须是JPG格式！')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片大小不能超过2MB！')
    return false
  }
  return true
}

const formDetail = reactive({value: [], dialog: false})

//弹窗展示编辑表格
function showDetail(row: any) {
  console.log(row)
  Object.assign(formDetail, {value: JSON.parse(row.dishList), dialog: true});
}


//弹窗展示编辑表格
function finishInitialize(row: any) {
  console.log(row)
  Object.assign(formFinish, row);
  formFinish.status=2
  formFinish.dialog = true;
  formFinish.PopupType = PopupType.value[1];
}


function ConfirmFinish() {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnConfirmAble.value = true
  if (formFinish.PopupType == PopupType.value[0]) {
    addMenuApi(formFinish)
        .then(function (response: any) {
          if (response.code === 200) {
            getTableData()
            formFinish.dialog = false;
            ElMessage.success(response.message)
          } else {
            ElMessage.warning("名称已使用")
          }
          loading.close()
        }).catch(response => {
      ElMessage.error("修改失败")
      loading.close()
    })
  } else {
    editOrderApi(formFinish).then(function (response: any) {
      if (response.code === 200) {
        getTableData()
        formFinish.dialog = false;
        ElMessage.success(response.message)
      } else {
        ElMessage.warning(response.message)
      }
      loading.close()
    }).catch(() => {
      ElMessage.error("修改失败")
      loading.close()
    })
  }

}


//添加系统用户


function addInitialize() {
  Object.assign(formFinish, {
    businessId: getToken(),
    description: "",
    id: null,
    image: "",
    name: "",
    price: 0,
    PopupType: PopupType.value[0],
    dialog: true
  });
}

const formDelete = reactive({
  id: -1,
  name: '',
  dialog: false
})

//删除
function deleteInitialize(row: any) {
  formDelete.id = row.id
  formDelete.name = row.name
  formDelete.dialog = true;
}

function Confirmdelete() {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnConfirmAble.value = true
  deleteMenuApi(formDelete.id)
      .then(function (response: any) {
        if (response.code === 200) {
          getTableData()
          ElMessage.success("删除成功")
        } else {
          ElMessage.error(response.message)
        }
        loading.close()
      }).catch(response => {
    ElMessage.error("删除失败")
    loading.close()
  })
}

const formLabelWidth = '140px'
const inputSize = 'default'

</script>
